package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.TransportCatalog;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.TransportCatalogDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2019-05-18 20:14:00
 */
public class TransportCatalogConverts {

    private static Logger LOG = LoggerFactory.getLogger(TransportCatalogConverts.class);

    public static List<TransportCatalogDTO> convert(List<TransportCatalog> transportCatalogList) {
        if (CollectionUtils.isEmpty(transportCatalogList)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportCatalogDTO> transportCatalogListResult = Lists.newArrayList();
        for (TransportCatalog transportCatalog : transportCatalogList) {
            transportCatalogListResult.add(convert(transportCatalog));
        }
        return transportCatalogListResult;
    }

    public static TransportCatalogDTO convert(TransportCatalog transportCatalog) {
        try {
            return BeanUtils.copy(transportCatalog, TransportCatalogDTO.class);
        } catch (Exception e) {
            LOG.error("TransportCatalog到TransportCatalogDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportCatalog到TransportCatalogDTO转换失败", e);
        }
    }

    public static TransportCatalog convert(TransportCatalogDTO transportCatalogDTO) {
        try {
            return BeanUtils.copy(transportCatalogDTO, TransportCatalog.class);
        } catch (Exception e) {
            LOG.error("TransportCatalogDTO到TransportCatalog转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportCatalogDTO到TransportCatalog转换失败", e);
        }
    }
}